<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Payaccount extends AdminController{
	public $_validation = null;
	
	function __construct(){
		parent::__construct();
		$this->load->model('MPayAccount');
		$this->_validation = Array(
			array('field'=>'account', 'label'=>'付款账号', 'rules'=>'required|max_length[128]')
			,array('field'=>'name', 'label'=>'账号所有者', 'rules'=>'required|max_length[32]')
            ,array('field'=>'detail_info', 'label'=>'账号详情', 'rules'=>'max_length[255]')
			,array('field'=>'remark', 'label'=>'备注', 'rules'=>'max_length[255]')
            ,array('field'=>'status', 'label'=>'状态', 'rules'=>'integer')
            ,array('field'=>'update_time', 'label'=>'更新时间', 'rules'=>'')
		);
	}

	function index(){
		//$this->output->enable_profiler(TRUE);
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		$this->formfilter->addFilter('account', 'where');
		$this->formfilter->addFilter('name', 'where');
		$limit = $this->pagination($this->MPayAccount->getCount());
		$list  = $this->MPayAccount->getList($limit);
		$this->assign('list', $list);
		$this->layout();
	}
	
	function add(){
	    $obj = $this->MPayAccount->createVo();
	    $this->load->library('form_validation');
	    $this->form_validation->set_rules($this->_validation);
	    if($this->form_validation->run()==TRUE){
	        $this->load->helper('populate');
	        $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
	        if($this->MPayAccount->add($newObj)){
	            successAndRedirect(l('edit_success'));
	        }else{
	            errorAndRedirect(l('data_fail'));
	        }
	    }
	    
	    $this->assign('obj', $obj);
	    $this->layout();
	}
	
	function edit($id){
	    if(!is_numeric($id)){
	        show_error(l('id_not_null'));
	    }
	    $obj = $this->MPayAccount->getOne(Array('id'=>$id));

	    if(!$obj){
	        errorAndRedirect('未找到定义！');
	    }
	    $this->editObj = $obj;
	    $this->load->library('form_validation');
	    $this->form_validation->set_rules($this->_validation);
	    if($this->form_validation->run()==TRUE){
	        $this->load->helper('populate');
	        $newObj = populate((Object)((Array)$obj), $this->form_validation->post());

	        if($this->MPayAccount->update($newObj)){
	            successAndRedirect(l('edit_success'));
	        }else{
	            errorAndRedirect(l('data_fail'));
	        }
	    }
	    $this->assign('obj', $obj);
	    $this->layout();
	}
	function delete($id, $update_time){
	    if(!is_numeric($id) || !is_numeric($update_time)){
	        show_error(l('id_not_null'));
	    }
	    if(!$this->MPayAccount->delete($id, $update_time)){
	        errorAndRedirect(l('data_fail'));
	    }
	    successAndRedirect(l('delete_success'));
	}
	
	
}
